<template>
  <div>
    <TopSwiper :tops="tops"></TopSwiper>
    <Card v-for='book in books' :key="book.id" :book="book"></Card>
    <p class="text-footer" v-if="!more">没有更多数据</p>
  </div>
</template>
<script>
import { get } from '@/util.js'
import Card from '@/components/Card.vue'
import TopSwiper from '@/components/TopSwiper.vue'
export default {
  data () {
    return {
      books: [],
      page: 0,
      more: true,
      tops: []
    }
  },
  components: {
    Card,
    TopSwiper
  },
  methods: {
    async getList (init) {
      if (init) {
        this.page = 0
        this.more = true
      }
      wx.showNavigationBarLoading()
      const books = await get('/weapp/booklist', {page: this.page})
      if (books.data.data.list.length < 10 && books.data.data.list.length > 0) {
        this.more = false
      }
      if (init) {
        this.books = books.data.data.list
      } else {
        // 下拉是累加
        this.books = this.books.concat(books.data.data.list)
      }
      wx.stopPullDownRefresh()
      wx.hideNavigationBarLoading()
    },
    async getTop () {
      const tops = await get('/weapp/top')
      this.tops = tops.data.data.list
    }
  },
  onPullDownRefresh () {
    this.getList(true)
    this.getTop()
  },
  onReachBottom () {
    if (!this.more) {
      return false
    } else {
      this.page += 1
      this.getList()
    }
  },
  mounted () {
    this.getList(true)
    this.getTop()
  }
}
</script>
<style>

</style>
